<template>
  <div>
    <div class="toolbar">
      <el-input-number v-model="courseId" :min="0" placeholder="课程ID" />
      <el-input-number v-model="termId" :min="0" placeholder="学期ID" />
      <el-input-number v-model="classId" :min="0" placeholder="班级ID" />
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="success" @click="openCreate">新增开课</el-button>
    </div>

    <el-table :data="items" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="courseId" label="课程ID" width="110" />
      <el-table-column prop="termId" label="学期ID" width="110" />
      <el-table-column prop="classId" label="班级ID" width="110" />
      <el-table-column prop="teacherId" label="教师ID" width="110" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="openEdit(row)">编辑</el-button>
          <el-popconfirm title="确认删除该开课记录？" @confirm="doDelete(row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="editing ? '编辑开课' : '新增开课'" width="560">
      <el-form :model="form" label-width="100px">
        <el-form-item label="课程">
          <el-select v-model="form.courseId" placeholder="选择课程" filterable style="width: 240px">
            <el-option v-for="c in courseOptions" :key="c.id" :label="c.name" :value="c.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="学期">
          <el-select v-model="form.termId" placeholder="选择学期" filterable style="width: 240px">
            <el-option v-for="t in termOptions" :key="t.id" :label="t.name" :value="t.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="班级">
          <el-select v-model="form.classId" placeholder="选择班级" filterable style="width: 240px">
            <el-option v-for="cl in classOptions" :key="cl.id" :label="cl.name" :value="cl.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="教师ID"><el-input-number v-model="form.teacherId" :min="0" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { listOfferings, createOffering, updateOffering, deleteOffering, type CourseOffering } from '@/api/grades/offerings'
import { listCourses, type Course } from '@/api/grades/courses'
import { listTerms, type Term } from '@/api/grades/terms'
import { listClasses, type Clazz } from '@/api/grades/classes'

const loading = ref(false)
const items = ref<CourseOffering[]>([])
const courseId = ref<number | undefined>(undefined)
const termId = ref<number | undefined>(undefined)
const classId = ref<number | undefined>(undefined)

const dialogVisible = ref(false)
const editing = ref(false)
const form = ref<CourseOffering>({})

const courseOptions = ref<Course[]>([])
const termOptions = ref<Term[]>([])
const classOptions = ref<Clazz[]>([])

async function load() {
  loading.value = true
  try {
    items.value = await listOfferings({ courseId: courseId.value, termId: termId.value, classId: classId.value })
  } finally {
    loading.value = false
  }
}

function openCreate() {
  editing.value = false
  form.value = {}
  dialogVisible.value = true
}

function openEdit(row: CourseOffering) {
  editing.value = true
  form.value = { ...row }
  dialogVisible.value = true
}

async function submit() {
  try {
    if (editing.value && form.value.id) {
      await updateOffering(form.value.id, form.value)
      ElMessage.success('更新成功')
    } else {
      await createOffering(form.value)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    await load()
  } catch (e: any) {
    ElMessage.error(e?.message || '操作失败')
  }
}

async function doDelete(id?: number) {
  if (!id) return
  try {
    await deleteOffering(id)
    ElMessage.success('已删除')
    await load()
  } catch (e: any) {
    ElMessage.error(e?.message || '删除失败')
  }
}

async function loadCourseOptions() {
  try { courseOptions.value = await listCourses({ limit: 1000 }) } catch {}
}
async function loadTermOptions() {
  try { termOptions.value = await listTerms({}) } catch {}
}
async function loadClassOptions() {
  try { const res = await listClasses({ pageNum: 1, pageSize: 1000 }); classOptions.value = res.items } catch {}
}

onMounted(() => {
  load()
  loadCourseOptions()
  loadTermOptions()
  loadClassOptions()
})
</script>

<style scoped>
.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}
</style>
